﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LosScrum.Models;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;
using LosScrum.Dominio;
using System.Text;

namespace LosScrum.Controllers
{
    public class TipoController : Controller
    {
        private string tiporecursoRESTService = "http://localhost:33348/TipoRecursos.svc/TipoRecurso";
        JavaScriptSerializer js = new JavaScriptSerializer();


        //private List<Tipo> CrearTipos()
        //{
        //    //Carlos Mayta
        //    List<Tipo> tipos = new List<Tipo>();

        //    tipos.Add(new Tipo() { Codigo = 1, Nombre = "CUBICULO"});
        //    tipos.Add(new Tipo() { Codigo = 2, Nombre = "PC" });
        //    tipos.Add(new Tipo() { Codigo = 3, Nombre = "LABORATORIO" });
        //    return tipos;
        //}

        //private Tipo ObtenerTipos(int codigo)
        //{  
        //    List<Tipo> tipos = (List<Tipo>)Session["tipos"];

        //    Tipo model = tipos.Single(delegate(Tipo tipo)
        //    {
        //        if (tipo.Codigo == codigo) return true;
        //        else return false;
        //    });
        //    return model;



        //}

        //
        // GET: /Recursos/

        public ActionResult Index()
        {
            //if (Session["tipos"] == null)
            //    Session["tipos"] = CrearTipos();
            //List<Tipo> model = (List<Tipo>)Session["tipos"];
            //return View(model);

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(tiporecursoRESTService);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string materialesJson = reader.ReadToEnd();
            List<TipoRecurso> materiales = js.Deserialize<List<TipoRecurso>>(materialesJson);
            return View(materiales);
        }

        //
        // GET: /Recursos/Details/5

        public ActionResult Details(int id)
        {
            //Tipo model = ObtenerTipos(id);
            //return View(model);

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(tiporecursoRESTService + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string materialJson = reader.ReadToEnd();
            TipoRecurso material = js.Deserialize<TipoRecurso>(materialJson);
            return View(material);
        }

        //
        // GET: /Recursos/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Recursos/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            //try
            //{
            //    List<Tipo> tipos = (List<Tipo>)Session["tipos"];
            //    tipos.Add(new Tipo()
            //    {
            //        Codigo = int.Parse(collection["Codigo"]),
            //        Nombre= collection["Nombre"],
            //    });

            //    return RedirectToAction("Index");
            //}
            //catch
            //{
            //    return View();
            //}

            try
            {
                string postdata = "{\"Codigo\":\"" + collection["Codigo"] + "\",\"Nombre\":\"" + collection["Nombre"] + "\",\"Estado\":\"" + collection["Estado"] + "\"}";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(tiporecursoRESTService);
                req.Method = "POST";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                //ModelState.AddModelError("Error", e.Message);
                //return View();


                TempData["message"] = "TIPO DE RECURSO YA EXISTE!!!!!!!!!!!!!!!!!!!!!!!";
                return (RedirectToAction("Create"));
            }
        }
        
        //
        // GET: /Recursos/Edit/5
 
        public ActionResult Edit(int id)
        {
            //Tipo model = ObtenerTipos(id);
            //return View(model);

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(tiporecursoRESTService + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string materialJson = reader.ReadToEnd();
            TipoRecurso material = js.Deserialize<TipoRecurso>(materialJson);
            return View(material);
        }

        //
        // POST: /Recursos/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            //try
            //{
            //    Tipo model = ObtenerTipos(id);
            //    model.Codigo= int.Parse(collection["Codigo"]);
            //    model.Nombre = collection["Nombre"];
            //    return RedirectToAction("Index");
            //}
            //catch
            //{
            //    return View();
            //}

            try
            {
                string postdata = "{\"Codigo\":\"" + id + "\",\"Nombre\":\"" + collection["Nombre"] + "\"}";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(tiporecursoRESTService);
                req.Method = "PUT";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }

        }

        //
        // GET: /Recursos/Delete/5
 
        public ActionResult Delete(int id)
        {
            //Tipo model = ObtenerTipos(id);
            //return View(model);

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(tiporecursoRESTService + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string materialJson = reader.ReadToEnd();
            TipoRecurso material = js.Deserialize<TipoRecurso>(materialJson);
            return View(material);
        }

        //
        // POST: /Recursos/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            //try
            //{
            //    List<Tipo> tipos = (List<Tipo>)Session["tipos"];
            //    tipos.Remove(ObtenerTipos(id));
            //    return RedirectToAction("Index");
            //}
            //catch
            //{
            //    return View();
            //}

            try
            {
                string postdata = "\"" + id + "\"";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(tiporecursoRESTService + "/" + id);
                req.Method = "DELETE";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                return View();
            }
        }
    }
}
